package com.example.administrator.xiaoxiang.activity;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.xiaoxiang.R;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    private Button getCode;//获取验证码
    private Button login;
    private EditText phoneNumber;
    private EditText codeNumber;
    private EventHandler eh;
    private int count = 60;
    private ImageView imageView;
    private EditText editText;
    private boolean flag = false;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x110) {
                count --;
                if (count<=0) {
                    getCode.setText("发送验证码");
                    getCode.setTextColor(Color.RED);
                    getCode.setEnabled(true);
                    count = 60;
                }else{
                    getCode.setText("重新发送（"+count+"）");
                    getCode.setTextColor(Color.GRAY);
                    getCode.setEnabled(false);
                    handler.sendEmptyMessageDelayed(0x110,1000);
                }
            }else if(msg.what == 0x120){
                getCode.setText("发送验证码");
                Toast.makeText(RegisterActivity.this, "验证码不正确", Toast.LENGTH_SHORT).show();
                count = 60;
                getCode.setEnabled(true);
            }else if(msg.what == 0x130){
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                MainActivity.phoneNum = phoneNumber.getText().toString();
                MainActivity.password = editText.getText().toString();
                finish();
                return;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        SMSSDK.initSDK(RegisterActivity.this, "14e3dbf5f02f7", "30583cbc9f9a48ebd63a08bbe839215d");
        init();
        eh = new EventHandler(){

            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_ERROR) {
                    handler.sendEmptyMessage(0x120);
                }
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功

                        handler.sendEmptyMessage(0x130);
                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        handler.sendEmptyMessage(0x110);
                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        //返回支持发送验证码的国家列表
                    }
                }else{
                    ((Throwable)data).printStackTrace();
                }
            }
        };
        SMSSDK.registerEventHandler(eh);
    }
    private void init() {
        getCode = (Button) findViewById(R.id.login_request_code_btn);
        login = (Button) findViewById(R.id.login_commit_btn);
        phoneNumber = (EditText) findViewById(R.id.login_input_phone_et);
        codeNumber = (EditText) findViewById(R.id.login_input_code_et);
        imageView = (ImageView) findViewById(R.id.hide);
        editText = (EditText) findViewById(R.id.login_input_code_et2);
        imageView.setOnClickListener(this);
        getCode.setOnClickListener(this);
        login.setOnClickListener(this);
        phoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().matches("^[1][34578]\\d{9}$")) {
                    getCode.setTextColor(Color.RED);
                    getCode.setEnabled(true);
                }else{
                    getCode.setTextColor(Color.GRAY);
                    getCode.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        String s =  phoneNumber.getText().toString();
        String code = codeNumber.getText().toString();
        String password = editText.getText().toString();
        switch (v.getId()){
            case R.id.login_request_code_btn:
                if (s.matches("^[1][34578]\\d{9}$")) {//初步验证是否是手机号码
                    SMSSDK.getVerificationCode("86",s);
                    Toast.makeText(RegisterActivity.this, "正在生成验证码。。。", Toast.LENGTH_SHORT).show();
                    getCode.setText("请稍候。。");
                }else{
                    Toast.makeText( RegisterActivity.this,"请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.login_commit_btn:
                Log.e(TAG, "onClick: "+code);
                if (s.matches("^[1][34578]\\d{9}$")){
                    if (password.length()>3)
                        SMSSDK.submitVerificationCode("86", s,code);
                    else
                        Toast.makeText(this, "~密码要大于3位~", Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(RegisterActivity.this, "手机号与验证码不匹配", Toast.LENGTH_SHORT).show();
                break;
            case R.id.hide:
                flag = !flag;
                if (flag) {
                    editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    editText.setSelection(editText.getText().toString().length());
                    imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.show));
                }else{
                    editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD|InputType.TYPE_CLASS_TEXT);
                    editText.setSelection(editText.getText().toString().length());
                    imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.hide));
                }
                break;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        SMSSDK.unregisterEventHandler(eh);
    }
    public void click(View view) {
            finish();

    }
}
